package BankingManagement;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class BankingService {
    private HashMap<String, BankAccount> accounts;
    private TaskManager taskManager;

    public BankingService() {
        accounts = new HashMap<>();
        taskManager = new TaskManager();
    }

    /**
     * Creates a new bank account
     *
     * @param accountNumber  Unique account identifier
     * @param initialBalance Starting balance
     * @param rate
     */
    public void createAccount(String accountNumber, double initialBalance, double rate) {
        if (accounts.containsKey(accountNumber)) {
            throw new IllegalArgumentException("Account already exists");
        }
        accounts.put(accountNumber, new BankAccount(initialBalance, rate));
        taskManager.addTask("Create account " + accountNumber + "*");
    }

    /**
     * Deposits money into an account
     * @param accountNumber Target account number
     * @param amount Amount to deposit
     * @return true if deposit successful, false otherwise
     */
    public boolean deposit(String accountNumber, double amount) {
        if (amount <= 0) return false;

        BankAccount account = accounts.get(accountNumber);
        if (account == null) return false;

        account.deposit(amount);
        taskManager.addTask("Deposit RMB " + amount + " to " + accountNumber + "*");
        return true;
    }

    /**
     * Withdraws money from an account
     * @param accountNumber Target account number
     * @param amount Amount to withdraw
     * @return true if withdrawal successful, false otherwise
     */
    public boolean withdraw(String accountNumber, double amount) {
        if (amount <= 0) return false;

        BankAccount account = accounts.get(accountNumber);
        if (account == null || account.getBalance() < amount) return false;

        account.withdraw(amount);
        taskManager.addTask("Withdraw RMB " + amount + " from " + accountNumber + "*");
        return true;
    }

    /**
     * Gets current account balance
     * @param accountNumber Target account number
     * @return Account balance
     */
    public double getBalance(String accountNumber) {
        BankAccount account = accounts.get(accountNumber);
        return account != null ? account.getBalance() : -1;
    }

    /**
     * Gets list of high priority tasks
     * @return List of high priority task strings
     */
    public ArrayList<String> listHighPriorityTasks() {
        return taskManager.getHighPriorityTasks();
    }

    /**
     * Gets account details as formatted string
     */
    public String getAccountDetails(String accountNumber) {
        BankAccount account = accounts.get(accountNumber);
        if (account == null) return "Account not found";

        return String.format("Account: %s\nBalance: RMB %.2f\nDeposits: %d\nWithdrawals: %d\nInterest Rate: %.2f%%",
                accountNumber,
                account.getBalance(),
                account.getDepositsCount(),
                account.getWithdrawalsCount(),
                account.getAnnualInterestRate());
    }

    /**
     * Lists all account numbers
     */
    public List<String> listAllAccounts() {
        return new ArrayList<>(accounts.keySet());
    }

    /**
     * Gets low priority tasks
     */
    public ArrayList<String> listLowPriorityTasks() {
        return taskManager.getLowPriorityTasks();
    }

    /**
     * Adds a manual task
     */
    public void addManualTask(String task) {
        taskManager.addTask(task);
    }

    /**
     * Removes a task
     */
    public void removeTask(String priority, int index) {
        taskManager.removeTask(priority, index);
    }

    /**
     * Changes task priority
     */
    public void changeTaskPriority(String priority, int index) {
        taskManager.changePriority(priority, index);
    }
}